/***
 * dynaContent
 * Sysnet Pioneer PL : dynaContent
 * MIT License
 * 
 * @desc Plugins that creates dynamic container. The url is the basis of the content.
 * @dependencies jQuery
 * @dependencies jQBBQ
 * 
 * Author : Darkarts
 */

var dyna_content_xhr
,dyna_content_post = {
        
    lol : "hehe"
        
};

(function($){
    
    $.set.dyna_content = {
        base_url:"r=public/index"
        ,
        base_page:"index.jsp"
        ,
        on_request:fn
        ,
        on_hashchange:fn
        ,
        success:fn
        ,
        error:fn
        ,
        re_req:false
        ,
        has_template:false
        ,
        limit:20
    };
    
    /**
     * dyna content 
     * @param args
     *  @args on_request
     *  @args base_url
     *  @args re_req - the url will be re requested if set to true
     */
    
    $.fn.dyna_content = function(args){
        
        
        
        //        con.log("set =>",set);
        
        return this.each(function(){
            var $this = $(this);
                        
            $(window).bind("hashchange",function(){
                // init
                
                
                var set = $.extend({
                    get_url : function(){

                        var hash_full   = location.hash;
                        var start_index = (hash_full.indexOf("!") == 1) ? 2 : 1;
                        var wo_hasTemplate_param = hash_full.replace(/hasTemplate=true/i,"hasTemplate=false");
                        
                        var hash_val    = (wo_hasTemplate_param.substr(start_index)) ? (wo_hasTemplate_param.substr(start_index)) : this.base_url;
                        
                        hash_val    += "&hasTemplate="+this.has_template; // hasTemplate                

                        return this.base_page+"?"+hash_val;
                    }

                },$.set.dyna_content,args);

                // Triggers

                $this.bind_once("success",set.success);
                $this.bind_once("error",set.error);
                $this.bind_once("on_request",set.on_request);
                $this.bind_once("on_hashchange",set.on_hashchange);
                // !Triggers
                
                
                var hash_full = location.hash;
                var response;
                
                $("input[href='"+hash_full+"']").trigger("click");// click the element that has the hash_full key.
                $this.trigger("on_hashchange");
                
                
                if(_page.is_stored() && set.re_req != true){
                    
                    response = _page.get_current();
                    $this.html(response);
                    $this.trigger("success");
                //                    set.on_request($this)

                }else if($this.triggerHandler("on_request") !== false){
                    
                    if(dyna_content_xhr != null)dyna_content_xhr.abort();
                    
                    dyna_content_xhr = $.ajax({
                        url:set.get_url()
                        ,
                        data:dyna_content_post
                        ,
                        success:function(a,b,c){
                            _page.store(a);
                            
                            
                            dyna_content_xhr = null;
                            
                            $this.html(a);
                            $this.trigger("success"); //triggers when then request is success.
                        }
                        ,
                        error:function(){
                            $this.trigger("error");
                        }
                    });
                    
                }

            });
            
            
        });        
    };
})(jQuery);